﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Invoicing.Model.Products;
namespace Invoicing.Command.Products
{
    public class ProductProcessCommand
    {
        public static ProductProcessCollection GetAll()
        {
            var ret = new ProductProcessCollection();
            using (DBCentre db = new DBCentre())
            {
                IQueryable<ProductProcess> q = db.DataContext.ProductProcesses;
                var n = q.Count();
                if (n > 0)
                {
                    q = q.OrderBy(p => p.ID);
                    var lst = q.ToList();
                    ret = Convertor.ConvertCollection<ProductProcessModel, ProductProcess, ProductProcessCollection>(lst);
                    ret.TotalRecordsCount = n;
                }
            }
            return ret;
        }

        public static ProductProcessModel GetModel(int id)
        {
            using (DBCentre db = new DBCentre())
            {
                var d = db.DataContext.ProductProcesses.FirstOrDefault(p => p.ID == id);
                if (d != null)
                {
                    var ret = Convertor.ConvertTo<ProductProcessModel, ProductProcess>(d);
                    return ret;
                }
            }
            return null;
        }
    }
}
